package com.wesmart.mvvm.share

import android.app.SharedElementCallback
import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import com.skyjing.askit.LogUtils
import com.skyjing.astools.databinding.ActivitySharaElementBinding
import com.skyjing.astools.databinding.ActivityShare2Binding

class ShareTwoActivity : AppCompatActivity() {
    private lateinit var binding: ActivityShare2Binding
    private var index = 0
    private var click = 0
    private var type = 0

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityShare2Binding.inflate(LayoutInflater.from(this))
        setContentView(binding.root)

        index = intent.getIntExtra("index", 0)
        click = intent.getIntExtra("click", 0)
        type = intent.getIntExtra("type", 0)
//        supportPostponeEnterTransition()//延缓执行 然后在fragment里面的控件加载完成后start
        if (Build.VERSION.SDK_INT >= 22) {
            setEnterSharedElementCallback(object : SharedElementCallback() {
                override fun onMapSharedElements(names: MutableList<String>?, sharedElements: MutableMap<String, View>?) {
//                    super.onMapSharedElements(names, sharedElements)
                    //这个可以看做个管道  每次进入和退出的时候都会进行调用  进入的时候获取到前面传来的共享元素的信息
                    //退出的时候 把这些信息传递给前面的activity
                    //同时向sharedElements里面put view,跟对view添加transitionname作用一样
                    sharedElements?.clear()
//                    sharedElements?.put("item$index", binding.iv1)
                    LogUtils.e("共享元素", "ShareTwoActivity item$index")
                }
            })
        }
//        startPostponedEnterTransition()

//        binding.iv1.transitionName = "wang"
    }

//    override fun supportFinishAfterTransition() {
//        val data = Intent()
//        if (index == 1) {
//            data.putExtra("index", 2)
//            index = 2
//        } else {
//            index = 1
//            data.putExtra("index", index)
//        }
////        data.putExtra("index", 2)
//        LogUtils.e("共享元素", "supportFinishAfterTransition item$index")
//        setResult(RESULT_OK, data)
//        super.supportFinishAfterTransition()
//    }

    override fun onBackPressed() {
        super.onBackPressed()
        val data = Intent()
        data.putExtra("index", index)
        data.putExtra("click",click)
        data.putExtra("type",type)

        LogUtils.e("共享元素", "onBackPressed item$index")
        setResult(RESULT_OK, data)
        super.supportFinishAfterTransition()
//        finish()
    }

}